package com.baizhi.wwx.service;

import com.baizhi.wwx.dao.CategoryMapper;

import com.baizhi.wwx.entity.Category;
import com.baizhi.wwx.entity.CategoryExample;

import com.baizhi.wwx.vo.CategoryVo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public CategoryVo queryOnePage(Integer page, Integer pageSize) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(1);
        RowBounds rowBounds=new RowBounds((page-1)*pageSize,pageSize);
        List<Category>categories=categoryMapper.selectByExampleAndRowBounds(categoryExample,rowBounds);
        int total=categoryMapper.selectCountByExample(categoryExample);
        return new CategoryVo(total,page,categories);
    }

    @Override
    public CategoryVo queryTwoPage(Integer page, Integer pageSize,Integer categoryId) {
        CategoryExample categoryExample=new CategoryExample();
        categoryExample.createCriteria().andParentIdEqualTo(categoryId);
        RowBounds rowBounds=new RowBounds((page-1)*pageSize,pageSize);
        List<Category>categories=categoryMapper.selectByExampleAndRowBounds(categoryExample,rowBounds);
        int total=categoryMapper.selectCountByExample(categoryExample);
        return new CategoryVo(total,page,categories);
    }

    @Override
    public Category queryById(Integer id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Category> queryByLevelsCategory(Integer levels) {
        CategoryExample categoryExample=new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(levels);
        List<Category>list=categoryMapper.selectByExample(categoryExample);
        return list;
    }

    @Override
    public Map<String, Object> add(String cateName, Integer parentId) {
        Map<String ,Object>map=new HashMap<>();
        if (parentId==null){
            Category category=new Category(null,cateName,1,parentId);
            int i = categoryMapper.insertSelective(category);
            if(i==1){
                map.put("message","添加成功");
                map.put("status",200);
            }else {
                map.put("message","添加失败");
                map.put("status",400);
            }
        }else {
            Category category=new Category(null,cateName,2,parentId);
            int i = categoryMapper.insertSelective(category);
            if(i==1){
                map.put("message","添加成功");
                map.put("status",200);
            }else {
                map.put("message","添加失败");
                map.put("status",400);
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> update(Integer id, String cateName, Integer parentId) {
        Map<String ,Object>map=new HashMap<>();
        if (parentId==null){
            Category category=new Category(id,cateName,1,parentId);
            int i = categoryMapper.updateByPrimaryKeySelective(category);
            if(i==1){
                map.put("message","修改成功");
                map.put("status",200);
            }else {
                map.put("message","修改失败");
                map.put("status",400);
            }
        }else {
            Category category=new Category(id,cateName,2,parentId);
            int i = categoryMapper.updateByPrimaryKeySelective(category);
            if(i==1){
                map.put("message","修改成功");
                map.put("status",200);
            }else {
                map.put("message","修改失败");
                map.put("status",400);
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> delete(Integer id, Integer parentId) {
        Map<String,Object>map=new HashMap<>();
        if(parentId != null){
            categoryMapper.deleteByPrimaryKey(id);
            map.put("message","删除成功");
            map.put("status",200);
        }else {
            CategoryExample categoryExample=new CategoryExample();
            categoryExample.createCriteria().andParentIdEqualTo(id);
            List<Category>list=categoryMapper.selectByExample(categoryExample);
            if (list.size()==0){
                categoryMapper.deleteByPrimaryKey(id);
                map.put("message","删除成功");
                map.put("status",200);
            }else {
                map.put("message","一级类别下有二级类别不能删除");
                map.put("status",400);
            }
        }
        return map;
    }


}
